/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author tea milandi
 */
public class PemesananBarang {

    private String kodePemesanan;
    private String waktuPemesanan;
    private double jumlahBarang;
    private String idBarang;
    private String username;
    private String status;
    private String kodebaru;
    String jdbcUrl = "jdbc:oracle:thin:@172.23.9.185:1521:orcl";
    String userid = "mhs115314008"; // gantikan dengan username anda
    String pass = "mhs115314008"; // gantikan dengan password anda

    public String getKodePemesanan() throws SQLException {
        Connection connection = null;
        OracleDataSource ds;
        ds = new OracleDataSource();
        ds.setURL(jdbcUrl);
        ds.setPassword(pass);
        ds.setUser(userid);
        connection = (Connection) ds.getConnection();
        Statement statement = connection.createStatement();
        SimpleDateFormat sdf1 = new SimpleDateFormat("dMyy");
        Date d = new Date();
        String finalDate = sdf1.format(d);
        String k1 = generateKodePemesanan(finalDate);
        String code = null;
        String kod = k1.substring(0, 6);
        int a = 0;
        Statement st = connection.createStatement();
        ResultSet rs = st.executeQuery("select kodePemesanan from pemesanan_asmart where kodePemesanan like '" + kod + "%'");
        while (rs.next()) {
            String sementara = rs.getString("kodePemesanan");
            String s2 = sementara.substring(6);
            a = Integer.parseInt(s2);
            a++;
        }
        return this.kodePemesanan = kod + String.valueOf(a);
    }

    public String getWaktuPemesanan() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d-M-y");
        this.waktuPemesanan = simpleDateFormat.format(date);
        return waktuPemesanan;
    }

    public double getJumlahBarang() {
        return jumlahBarang;
    }

    public void setJumlahBarang(double jumlahBarang) {
        this.jumlahBarang = jumlahBarang;
    }

    public String getIdBarang() {
        return idBarang;
    }

    public void setIdBarang(String idBarang) {
        this.idBarang = idBarang;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getStatus() {
        return status;
    }

    public void insertPemesanan() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            statement.executeQuery("insert into pemesanan_asmart values ('"
                    + getKodePemesanan() + "','"
                    + getWaktuPemesanan() + "',"
                    + getJumlahBarang() + ",'"
                    + getIdBarang() + "','"
                    + getUsername() + "','"
                    + getStatus() + "')");
        } catch (SQLException e) {
            System.out.println("error :" + e.getMessage());
        }
    }

    public ResultSet tampilPemesanan() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from pemesanan_asmart order by waktupemesanan, kodepemesanan desc");
            return resultSet;
        } catch (SQLException e) {
            System.out.println("error :" + e.getMessage());
            return null;
        }
    }

    public String tampilIdBarang(String kodePemesanan) {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select idbarang from pemesanan_asmart where kodepemesanan = '" + kodePemesanan + "'");
            while (resultSet.next()) {
                return resultSet.getString("idbarang");
            }
            return "0";
        } catch (SQLException e) {
            System.out.println("error :" + e.getMessage());
            return "0";
        }
    }

    public String generateKodePemesanan(String date) {
        String kode;
        kode = date + "0";
        return kode;
    }

    /**
     * @return the kodebaru
     */
    public String getKodebaru() {
        return kodebaru;
    }

    /**
     * @param kodebaru the kodebaru to set
     */
    public void setKodebaru(String kodebaru) {
        this.kodebaru = kodebaru;
    }

    public void updateStatus(Double jum, String kodepesan) throws SQLException {
        Connection connection = null;
        OracleDataSource ds;
        ds = new OracleDataSource();
        ds.setURL(jdbcUrl);
        ds.setPassword(pass);
        ds.setUser(userid);
        connection = (Connection) ds.getConnection();
        Statement statement = connection.createStatement();
        statement.executeQuery("update pemesanan_asmart set status_pengiriman='terkirim'"
                + " where kodepemesanan='" + kodepesan + "'");
    }
}
